using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using EmailCenter_SEK53.Models;

namespace EmailCenter_SEK53
{
    public partial class Alert : DevComponents.DotNetBar.Balloon
    {
        EmailAccount account;
        private IRepository _repository;
        frmMain mainform;

        private static BayesClassifier.Classifier classifier = new BayesClassifier.Classifier();

        public Alert(Form calledform)
        {
            InitializeComponent();
            mainform = calledform as frmMain;
            this.account = mainform.account;
            _repository = mainform._repository;

            // Create an e-mail classifier 
            for (int i = 1; i < 6; i++)
            {
                classifier.TeachCategory("facebook", new System.IO.StreamReader(String.Format(@".\TrainingData\facebook\facebook{0}.txt", i)));
                classifier.TeachCategory("spam", new System.IO.StreamReader(String.Format(@".\TrainingData\spam\spam{0}.txt", i)));
                classifier.TeachCategory("normal", new System.IO.StreamReader(String.Format(@".\TrainingData\normal\normal{0}.txt", i)));
            }
        }

        private string GetCategoryOfAnEmail(string emailContent)
        {
            Dictionary<string, double> score = classifier.Classify(emailContent);
            string minCatName = "normal";
            double minCatValue = 10000;
            foreach (string c in score.Keys)
            {
                if (Math.Abs(score[c]) < minCatValue)
                {
                    minCatValue = Math.Abs(score[c]);
                    minCatName = c;
                }
            }
            return minCatName;
        }

        private void linkSubject_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            EmailParser email = account.DownloadAndParseCurrentEmail(Application.StartupPath + "\\Email");
            int id = _repository.isAccountExist(account.Email);
            if (id < 0)
                return;

            int type;
            string typeStr = this.GetCategoryOfAnEmail(email.content);
            if (typeStr.CompareTo("facebook") == 0)
            {
                type = 2;
            }
            else if (typeStr.CompareTo("spam") == 0)
            {
                type = 3;
            }
            else
            {
                type = 1;
            }

            _repository.InsertNewEmail(type, email.From,
                            email.PartnerName,
                            email.Subject,
                            email.UtcDateTime.ToString(),
                            email.content, id);
            mainform.OnClickAlertSubject();
            //LoadEmailInfo();
        }

        private void linkNext_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Email email = account.GetNextEmail();
            if (email != null)
                this.SetEmailInfo(email.From, email.Subject);
        }

        private void linkPrev_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Email email = account.GetPreviousEmail();
            if (email != null)
                this.SetEmailInfo(email.From, email.Subject);
        }

        void LoadEmailInfo()
        {
            int n = account.GetNewEmailsCount();
            this.CaptionText = "You have " + n.ToString() + " new Email(s)";
            if (n > 0)
            {
                Email email = account.GetEmail(1);
                this.SetEmailInfo(email.From, email.Subject);
            }
        }
        
        public void SetEmailInfo(string mailfrom, string subject)
        {
            labelFrom.Text = "From: " + mailfrom;
            linkSubject.Text = subject;
        }

        private void Alert_Load(object sender, EventArgs e)
        {
            account.Connect();
            LoadEmailInfo();
        }

        private void Alert_FormClosed(object sender, FormClosedEventArgs e)
        {
            mainform.bAlertShowed = false;
            account.Close();
        }

        private void Alert_MouseLeave(object sender, EventArgs e)
        {
            //this.AutoCloseTimeOut = 10;
        }

        private void Alert_MouseEnter(object sender, EventArgs e)
        {
            this.AutoClose = true;
            this.AutoCloseTimeOut = 0;
        }
    }
}